//
//  SQScanCodeViewController.m
//  energy
//
//  Created by 苏青 on 2021/7/30.
//

#import "SQScanCodeViewController.h"
// 扫码工具
#import "WSLScanView.h"
#import "WSLNativeScanTool.h"

#import "SQCodeViewController.h"

@interface SQScanCodeViewController ()
@property (nonatomic, strong)WSLNativeScanTool * scanTool;
@property (weak, nonatomic) IBOutlet UIView *bgview;
@property (nonatomic, strong)WSLScanView * scanView;
@end

@implementation SQScanCodeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.scanView startScanAnimation];
    [self.scanTool sessionStartRunning];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.scanView stopScanAnimation];
    [self.scanView finishedHandle];
    [self.scanView showFlashSwitch:NO];
    [self.scanTool sessionStopRunning];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidNav = YES;
    [self makeUILayer];
}

#pragma mark - 页面搭建
-(void)makeUILayer{
    
    //构建扫描样式视图
    _scanView = [[WSLScanView alloc] initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, HEIGHT_SCREEN)];
    _scanView.scanRetangleRect = CGRectMake(W(60), W(120), W((self.view.frame.size.width - 2 * 60)),  W((self.view.frame.size.width - 2 * 60)));
    _scanView.colorAngle = [UIColor greenColor];
    _scanView.photoframeAngleW = 20;
    _scanView.photoframeAngleH = 20;
    _scanView.photoframeLineW = 2;
    _scanView.isNeedShowRetangle = YES;
    _scanView.colorRetangleLine = [UIColor whiteColor];
    _scanView.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    _scanView.animationImage = [UIImage imageNamed:@"scanLine"];
    _scanView.myQRCodeBlock = ^{
//        // 获取指定的Storyboard，name填写Storyboard的文件名
//        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//        // 从Storyboard上按照identifier获取指定的界面（VC），identifier必须是唯一的
//        WSLCreateQRCodeController *createQRCodeController = [storyboard instantiateViewControllerWithIdentifier:@"createQRCode"];
//        createQRCodeController.qrImage =  [WSLNativeScanTool createQRCodeImageWithString:@"https://www.jianshu.com/u/e15d1f644bea" andSize:CGSizeMake(250, 250) andBackColor:[UIColor whiteColor] andFrontColor:[UIColor orangeColor] andCenterImage:[UIImage imageNamed:@"piao"]];
//        createQRCodeController.qrString = @"https://www.jianshu.com/u/e15d1f644bea";
//        [weakSelf.navigationController pushViewController:createQRCodeController animated:YES];
    };
    LC_WEAK_SELF
    _scanView.flashSwitchBlock = ^(BOOL open) {
        [weakSelf.scanTool openFlashSwitch:open];
    };
    self.bgview.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.bgview addSubview:_scanView];
    
    //初始化扫描工具
    _scanTool = [[WSLNativeScanTool alloc] initWithPreview:self.bgview andScanFrame:_scanView.scanRetangleRect];
   
    _scanTool.scanFinishedBlock = ^(NSString *scanString) {
        NSLog(@"扫描结果 %@",scanString);
       
        if ([scanString containsString:@"http"]) {
            
            NSArray *array = [scanString componentsSeparatedByString:@"="]; //从字符A中分隔成2个元素的数组
            
            SQCodeViewController * codeVC = SQCodeViewController.new;
            codeVC.ipm_id = array[1];
            
            codeVC.type = @"1";
          
            [weakSelf pushVC:codeVC];
            

        } else {
            NSDictionary * dic = [weakSelf turnStringToDictionary:scanString];
        
            SQCodeViewController * codeVC = SQCodeViewController.new;
            codeVC.ipm_id = dic[@"ipm_id"];
            codeVC.type = @"1";
          
            [weakSelf pushVC:codeVC];
            

        }

        [weakSelf.scanTool sessionStopRunning];
        [weakSelf.scanTool openFlashSwitch:NO];
    };
    _scanTool.monitorLightBlock = ^(float brightness) {
        //NSLog(@"环境光感 ： %f",brightness);
        if (brightness < 0) {
            // 环境太暗，显示闪光灯开关按钮
            [weakSelf.scanView showFlashSwitch:YES];
        }else if(brightness > 0){
            // 环境亮度可以,且闪光灯处于关闭状态时，隐藏闪光灯开关
            if(!weakSelf.scanTool.flashOpen){
                [weakSelf.scanView showFlashSwitch:NO];
            }
        }
    };
    [_scanTool sessionStartRunning];
    [_scanView startScanAnimation];
}
- (void)dealloc{
    NSLog(@"扫描销毁");
}


-(NSDictionary *)turnStringToDictionary:(NSString *)turnString{
    NSData *turnData = [turnString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *turnDic = [NSJSONSerialization JSONObjectWithData:turnData options:NSJSONReadingMutableLeaves error:nil];
    return turnDic;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
